<?php

namespace app\batch;

use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\facade\Debug;
use app\models\{Coupon as Coupons, Order, OrderProduct, User as Users, Config};
use think\Db;
use think\facade\Log;

class Coupon extends Command
{
    protected $open = false;
    protected function configure()
    {
        $this->setName('Coupon')->setDescription('优惠券检测');
    }

    protected function execute(Input $input, Output $output)
    {
        $this->doCron();
        die();
    }

    protected function doCron()
    {
        ini_set('max_execution_time', 0);
        //新用户赠送优惠券
        $nyqcoun = (new Config())->where('name', 'nyqcoun')->value('value') ?? '1';
        (new Users())->where(['first' => 1])->chunk(10, function ($users) use ($nyqcoun) {
            $nyqcoun = (float)$nyqcoun;
            foreach ($users as $user) {
                $mycoupon = [
                    'user_id' => $user['id'],
                    'types' => 2,
                    'productid' => '1,2,3,4,5,6,7,8,9,10,11,12,13,14',
                    'number' => 1,
                    'getmoney' => $nyqcoun,
                    'money' => 0,
                    'status' => 1,
                    'remark' => '新用户赠送',
                    'system' => 4,
                    'text' => '新用户赠送',
                    'endtime' => date('Y-m-d H:i:s', time() + 86400 * 10)
                ];
                Db::startTrans();
                try {
                    if (!empty($mycoupon)) {
                        (new Coupons())->allowfield(true)->save($mycoupon);
                    }
                    (new Users())->where(['first' => $user['first'], 'id' => $user['id']])->update(['first' => 2]);
                    Db::commit();
                } catch (\Exception $e) {
                    dump($e->getMessage());
                    Db::rollback();
                }
            }
        });

        //邀请赠送优惠券
        $yqcoun = (new Config())->where('name', 'yqcoun')->value('value') ?? '';
        (new Users())->where(['isgave' => 1])->where('pid', '>', 0)->chunk(10, function ($users) use ($yqcoun) {
            $yqcoun = json_decode($yqcoun, true);
            if (!is_array($yqcoun)) {
                $yqcoun = [];
            }
            foreach ($users as $user) {
                $mycoupon = [];
                foreach ($yqcoun as $coupon) {
                    if ($coupon['money'] > 0 && $coupon['days'] > 0) {
                        $mycoupon[] = [
                            'user_id' => $user['pid'],
                            'getmoney' => $coupon['getmoney'],
                            'money' => $coupon['money'],
                            'status' => 1,
                            'remark' => '邀请好友赠送',
                            'system' => 2,
                            'text' => '本券由平台发放，最终解释权归平台所有',
                            'endtime' => date('Y-m-d H:i:s', time() + 86400 * $coupon['days'])
                        ];
                    }
                }
                Db::startTrans();
                try {
                    if (count($mycoupon) > 0) {
                        (new Coupons())->saveAll($mycoupon);
                    }
                    (new Users())->where(['isgave' => $user['isgave'], 'id' => $user['id']])->update(['isgave' => 2]);
                    Db::commit();
                } catch (\Exception $e) {
                    dump($e->getMessage());
                    Db::rollback();
                }
            }
        });

        //优惠券过期检测
        (new Coupons())->where(['status' => 1])->whereTime('endtime', '<', date('Y-m-d H:i:s'))->chunk(10, function ($lists) {
            foreach ($lists as $list) {
                (new Coupons())->where(['status' => 1, 'id' => $list['id']])->update(['status' => 2]);
            }
        });
    }
}
